<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/9
 * Time: 15:35
 */
if (!defined('ACCESS')) {
    exit('Access denied.');
}

define('ENV', 'dev');
if (ENV == 'dev') {
    define('HOST', '127.0.0.1');
    define('PORT', '3307');
    define('USER', 'root');
    define('PWD', 'usbw');
    define('DBNAME', 'jhdb');
} elseif (ENV == 'test') {
    define('HOST', '127.0.0.1');
    define('PORT', '3306');
    define('USER', 'root');
    define('PWD', 'jh!@#2016');
    define('DBNAME', 'jhdb');
} else {
    define('HOST', '127.0.0.1');
    define('PORT', '3306');
    define('USER', 'root');
    define('PWD', 'jh!@#2016');
    define('DBNAME', 'jhdb');
}

class AdminProducts extends Base
{

    private static $table_name = 'products';
    // 查询字段
    private static $columns = array(
        'products_id',
        'products_model',
        'products_name',
        'products_images',
        'products_small_images',
        'products_description',
        'products_price',
        'products_special_price',
        'products_currency',
        'products_desc',
        'products_color',
        'products_size',
        'products_sale_num',
        'products_sale_other',
        'products_index',
        'products_type',
        'type_name',
        'products_add_date');

    public static function getTableName()
    {
        return self::$table_name;
    }


    public static function getProductById($pid)
    {
        $condition = array("products_id" => $pid);
        $db = self::__instance();
        $product = $db->get(self::getTableName(), self::$columns, $condition);
        if ($product) {
            return $product;
        }
        return array();
    }


    public static function getDefProduct()
    {
        $condition = array("products_index" => 1);
        $db = self::__instance();
        $product = $db->get(self::getTableName(), self::$columns, $condition);
        if ($product) {
            return $product;
        }
        return array();
    }

    public static function getAllProduct()
    {
        $db = self::__instance();
        $products = $db->select(self::getTableName(), self::$columns);
        if ($products) {
            return $products;
        }
        return array();
    }

    public static function update($where, $data)
    {
        $db = self::__instance();
        $rt = $db->update(self::getTableName(), $data, $where);
        return $rt;
    }


    public static function replace($data)
    {

        $db = self::__instance();
        if (!is_array($data)) {
            return false;
        }
        $value_str = '';
        $i = 0;
        $sign = ',';
        foreach ($data as $key => $value) {


            $s1 = "'" . $value[0] . "'";
            $s2 = "'" . $value[1] . "'";
            $s3 = "'" . $value[2] . "'";
            $s4 = "'" . $value[3] . "'";
            $s5 = "'" . $value[4] . "'";
            $s6 = "'" . $value[5] . "'";
            $s7 = "'" . $value[6] . "'";
            $s8 = "'" . $value[7] . "'";
            $s9 = "'" . $value[8] . "'";
            $s10 = "'" . $value[9] . "'";
            $s11 = "'" . $value[10] . "'";
            $s12 = "'" . $value[11] . "'";
            $s13 = "'" . $value[12] . "'";
            $s14 = "'" . $value[13] . "'";
            $s15 = "'" . $value[14] . "'";
            $s16 = "'" . date('Y-m-d H:i:s') . "'";
            $i++;
            $value_str_s = '(' . $s1 . ',' . $s2 . ',' . $s3 . ',' . $s4 . ',' . $s5 . ',' . $s6 . ',' . $s7 . ',' . $s8 . ',' . $s9 . ',' . $s10 . ',' . $s11 . ',' . $s12 . ',' . $s13 . ',' . $s14 . ',' . $s15 . ',' . $s16 . ')';
            if ($i == count($data)) {
                $sign = '';
            }
            $value_str .= $value_str_s . $sign;


            if ($value[15] == '1' && $value[12] == '1') {//代表该产品是主商品，只需配置一套广告图即可

                $rt = self::getStore();
                if ($rt) {
                    self::updateAdvertImg($value[16]);
                } else {
                    self::addStore($value[16]);
                }
            }
        }


        $sql = "REPLACE INTO products (`products_id`,`products_model`,`products_name`,`products_small_images`,`products_description`,`products_price`,`products_special_price`,`products_currency`,`products_color`,`products_size`,`products_sale_num`,`products_sale_other`,`products_index`,`products_type`,`type_name`,`products_add_date`)
        VALUES " . $value_str;

        $rt = $db->exec($sql);
        return $rt;
    }


    static function updateAdvertImg($value)
    {
        $dsn = 'mysql:dbname=' . DBNAME . ';host=' . HOST . ';port=' . PORT;
        $user = USER;
        $password = PWD;
        $dbh = new PDO($dsn, $user, $password);
        $dbh->query("set names utf8");


        if (isset($value)) {
            $update_str = "'" . $value . "'";
        } else {
            $update_str = "''";
        }

        $sql = "UPDATE store SET `advert_imgs`  = " . $update_str;

        $rt = $dbh->exec($sql);
        return $rt;
    }

    static function addStore($value)
    {
        $dsn = 'mysql:dbname=' . DBNAME . ';host=' . HOST . ';port=' . PORT;
        $user = USER;
        $password = PWD;
        $dbh = new PDO($dsn, $user, $password);
        $dbh->query("set names utf8");


        $s1 = "''";


        $s2 = "''";


        $s3 = "''";


        $s4 = "''";


        $s5 = "''";


        $s6 = "''";


        $s7 = "''";


        if (isset($value)) {
            $s8 = "'" . $value . "'";
        } else {
            $s8 = "''";
        }

        $value_str_s = '(' . $s1 . ',' . $s2 . ',' . $s3 . ',' . $s4 . ',' . $s5 . ',' . $s6 . ',' . $s7 . ',' . $s8 . ')';

        $sql = "INSERT INTO  store (`title`,`kewords`,`description`,`width`,`height`,`min_height`,`store_footer`,`advert_imgs`) VALUES " . $value_str_s;

        $rt = $dbh->exec($sql);
        return $rt;
    }

    static function getStore()
    {
        $dsn = 'mysql:dbname=' . DBNAME . ';host=' . HOST . ';port=' . PORT;
        $user = USER;
        $password = PWD;
        $dbh = new PDO($dsn, $user, $password);
        $dbh->query("set names utf8");
        $sql = "SELECT * FROM store";

        $rt = $dbh->query($sql)->fetch();
        return $rt;
    }
}